<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/util/insert_splits.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/util</a> - insert_splits.cpp<span style="font-size: 80%;"> (source / <a href="insert_splits.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">51</td>
            <td class="headerCovTableEntry">59</td>
            <td class="headerCovTableEntryMed">86.4 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:50:33</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">5</td>
            <td class="headerCovTableEntry">5</td>
            <td class="headerCovTableEntryHi">100.0 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;algorithm&gt;</a>
<span class="lineNum">       2 </span>            : #include &lt;map&gt;
<span class="lineNum">       3 </span>            : #include &lt;sstream&gt;
<span class="lineNum">       4 </span>            : #include &lt;string&gt;
<span class="lineNum">       5 </span>            : #include &lt;utility&gt;
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : #include &quot;caffe/common.hpp&quot;
<span class="lineNum">       8 </span>            : #include &quot;caffe/util/insert_splits.hpp&quot;
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : namespace caffe {
<span class="lineNum">      11 </span>            : 
<span class="lineNum">      12 </span><span class="lineCov">          2 : void InsertSplits(const NetParameter&amp; param, NetParameter* param_split) {</span>
<span class="lineNum">      13 </span>            :   // Initialize by copying from the input NetParameter.
<span class="lineNum">      14 </span><span class="lineCov">          2 :   param_split-&gt;CopyFrom(param);</span>
<span class="lineNum">      15 </span>            :   param_split-&gt;clear_layer();
<span class="lineNum">      16 </span>            :   map&lt;string, pair&lt;int, int&gt; &gt; blob_name_to_last_top_idx;
<span class="lineNum">      17 </span>            :   map&lt;pair&lt;int, int&gt;, pair&lt;int, int&gt; &gt; bottom_idx_to_source_top_idx;
<span class="lineNum">      18 </span>            :   map&lt;pair&lt;int, int&gt;, int&gt; top_idx_to_bottom_count;
<span class="lineNum">      19 </span>            :   map&lt;pair&lt;int, int&gt;, float&gt; top_idx_to_loss_weight;
<span class="lineNum">      20 </span>            :   map&lt;pair&lt;int, int&gt;, int&gt; top_idx_to_bottom_split_idx;
<span class="lineNum">      21 </span>            :   map&lt;int, string&gt; layer_idx_to_layer_name;
<span class="lineNum">      22 </span><span class="lineCov">         21 :   for (int i = 0; i &lt; param.layer_size(); ++i) {</span>
<span class="lineNum">      23 </span>            :     const LayerParameter&amp; layer_param = param.layer(i);
<span class="lineNum">      24 </span><span class="lineCov">         19 :     layer_idx_to_layer_name[i] = layer_param.name();</span>
<span class="lineNum">      25 </span><span class="lineCov">         59 :     for (int j = 0; j &lt; layer_param.bottom_size(); ++j) {</span>
<span class="lineNum">      26 </span>            :       const string&amp; blob_name = layer_param.bottom(j);
<span class="lineNum">      27 </span><span class="lineCov">         20 :       if (blob_name_to_last_top_idx.find(blob_name) ==</span>
<span class="lineNum">      28 </span>            :           blob_name_to_last_top_idx.end()) {
<span class="lineNum">      29 </span><span class="lineNoCov">          0 :         LOG(FATAL) &lt;&lt; &quot;Unknown bottom blob '&quot; &lt;&lt; blob_name &lt;&lt; &quot;' (layer '&quot;</span>
<span class="lineNum">      30 </span><span class="lineNoCov">          0 :                    &lt;&lt; layer_param.name() &lt;&lt; &quot;', bottom index &quot; &lt;&lt; j &lt;&lt; &quot;)&quot;;</span>
<span class="lineNum">      31 </span>            :       }
<span class="lineNum">      32 </span><span class="lineCov">         20 :       const pair&lt;int, int&gt;&amp; bottom_idx = make_pair(i, j);</span>
<span class="lineNum">      33 </span><span class="lineCov">         20 :       const pair&lt;int, int&gt;&amp; top_idx = blob_name_to_last_top_idx[blob_name];</span>
<span class="lineNum">      34 </span><span class="lineCov">         20 :       bottom_idx_to_source_top_idx[bottom_idx] = top_idx;</span>
<span class="lineNum">      35 </span><span class="lineCov">         20 :       ++top_idx_to_bottom_count[top_idx];</span>
<span class="lineNum">      36 </span>            :     }
<span class="lineNum">      37 </span><span class="lineCov">         61 :     for (int j = 0; j &lt; layer_param.top_size(); ++j) {</span>
<span class="lineNum">      38 </span>            :       const string&amp; blob_name = layer_param.top(j);
<span class="lineNum">      39 </span><span class="lineCov">         21 :       blob_name_to_last_top_idx[blob_name] = make_pair(i, j);</span>
<span class="lineNum">      40 </span>            :     }
<span class="lineNum">      41 </span>            :     // A use of a top blob as a loss should be handled similarly to the use of
<span class="lineNum">      42 </span>            :     // a top blob as a bottom blob to another layer.
<span class="lineNum">      43 </span>            :     const int last_loss =
<span class="lineNum">      44 </span><span class="lineCov">         38 :         std::min(layer_param.loss_weight_size(), layer_param.top_size());</span>
<span class="lineNum">      45 </span><span class="lineCov">         19 :     for (int j = 0; j &lt; last_loss; ++j) {</span>
<span class="lineNum">      46 </span>            :       const string&amp; blob_name = layer_param.top(j);
<span class="lineNum">      47 </span><span class="lineNoCov">          0 :       const pair&lt;int, int&gt;&amp; top_idx = blob_name_to_last_top_idx[blob_name];</span>
<span class="lineNum">      48 </span><span class="lineNoCov">          0 :       top_idx_to_loss_weight[top_idx] = layer_param.loss_weight(j);</span>
<span class="lineNum">      49 </span><span class="lineNoCov">          0 :       if (top_idx_to_loss_weight[top_idx]) {</span>
<span class="lineNum">      50 </span><span class="lineNoCov">          0 :         ++top_idx_to_bottom_count[top_idx];</span>
<span class="lineNum">      51 </span>            :       }
<span class="lineNum">      52 </span>            :     }
<span class="lineNum">      53 </span>            :   }
<span class="lineNum">      54 </span><span class="lineCov">         21 :   for (int i = 0; i &lt; param.layer_size(); ++i) {</span>
<span class="lineNum">      55 </span>            :     LayerParameter* layer_param = param_split-&gt;add_layer();
<span class="lineNum">      56 </span><span class="lineCov">         38 :     layer_param-&gt;CopyFrom(param.layer(i));</span>
<span class="lineNum">      57 </span>            :     // Replace any shared bottom blobs with split layer outputs.
<span class="lineNum">      58 </span><span class="lineCov">         59 :     for (int j = 0; j &lt; layer_param-&gt;bottom_size(); ++j) {</span>
<span class="lineNum">      59 </span>            :       const pair&lt;int, int&gt;&amp; top_idx =
<span class="lineNum">      60 </span><span class="lineCov">         20 :           bottom_idx_to_source_top_idx[make_pair(i, j)];</span>
<span class="lineNum">      61 </span><span class="lineCov">         20 :       const int split_count = top_idx_to_bottom_count[top_idx];</span>
<span class="lineNum">      62 </span><span class="lineCov">         20 :       if (split_count &gt; 1) {</span>
<span class="lineNum">      63 </span><span class="lineCov">          4 :         const string&amp; layer_name = layer_idx_to_layer_name[top_idx.first];</span>
<span class="lineNum">      64 </span>            :         const string&amp; blob_name = layer_param-&gt;bottom(j);
<span class="lineNum">      65 </span><span class="lineCov">         12 :         layer_param-&gt;set_bottom(j, SplitBlobName(layer_name,</span>
<span class="lineNum">      66 </span><span class="lineCov">          8 :             blob_name, top_idx.second, top_idx_to_bottom_split_idx[top_idx]++));</span>
<span class="lineNum">      67 </span>            :       }
<span class="lineNum">      68 </span>            :     }
<span class="lineNum">      69 </span>            :     // Create split layer for any top blobs used by other layer as bottom
<span class="lineNum">      70 </span>            :     // blobs more than once.
<span class="lineNum">      71 </span><span class="lineCov">         61 :     for (int j = 0; j &lt; layer_param-&gt;top_size(); ++j) {</span>
<span class="lineNum">      72 </span><span class="lineCov">         21 :       const pair&lt;int, int&gt;&amp; top_idx = make_pair(i, j);</span>
<span class="lineNum">      73 </span><span class="lineCov">         21 :       const int split_count = top_idx_to_bottom_count[top_idx];</span>
<span class="lineNum">      74 </span><span class="lineCov">         21 :       if (split_count &gt; 1) {</span>
<span class="lineNum">      75 </span><span class="lineCov">          2 :         const string&amp; layer_name = layer_idx_to_layer_name[i];</span>
<span class="lineNum">      76 </span>            :         const string&amp; blob_name = layer_param-&gt;top(j);
<span class="lineNum">      77 </span>            :         LayerParameter* split_layer_param = param_split-&gt;add_layer();
<span class="lineNum">      78 </span><span class="lineCov">          2 :         const float loss_weight = top_idx_to_loss_weight[top_idx];</span>
<span class="lineNum">      79 </span>            :         ConfigureSplitLayer(layer_name, blob_name, j, split_count,
<span class="lineNum">      80 </span><span class="lineCov">          2 :             loss_weight, split_layer_param);</span>
<span class="lineNum">      81 </span><span class="lineCov">          2 :         if (loss_weight) {</span>
<span class="lineNum">      82 </span>            :           layer_param-&gt;clear_loss_weight();
<span class="lineNum">      83 </span><span class="lineNoCov">          0 :           top_idx_to_bottom_split_idx[top_idx]++;</span>
<span class="lineNum">      84 </span>            :         }
<span class="lineNum">      85 </span>            :       }
<span class="lineNum">      86 </span>            :     }
<span class="lineNum">      87 </span>            :   }
<a name="88"><span class="lineNum">      88 </span><span class="lineCov">          2 : }</span></a>
<span class="lineNum">      89 </span>            : 
<span class="lineNum">      90 </span><span class="lineCov">          2 : void ConfigureSplitLayer(const string&amp; layer_name, const string&amp; blob_name,</span>
<span class="lineNum">      91 </span>            :     const int blob_idx, const int split_count, const float loss_weight,
<span class="lineNum">      92 </span>            :     LayerParameter* split_layer_param) {
<span class="lineNum">      93 </span><span class="lineCov">          2 :   split_layer_param-&gt;Clear();</span>
<span class="lineNum">      94 </span>            :   split_layer_param-&gt;add_bottom(blob_name);
<span class="lineNum">      95 </span><span class="lineCov">          4 :   split_layer_param-&gt;set_name(SplitLayerName(layer_name, blob_name, blob_idx));</span>
<span class="lineNum">      96 </span><span class="lineCov">          2 :   split_layer_param-&gt;set_type(&quot;Split&quot;);</span>
<span class="lineNum">      97 </span><span class="lineCov">         10 :   for (int k = 0; k &lt; split_count; ++k) {</span>
<span class="lineNum">      98 </span>            :     split_layer_param-&gt;add_top(
<span class="lineNum">      99 </span><span class="lineCov">          8 :         SplitBlobName(layer_name, blob_name, blob_idx, k));</span>
<span class="lineNum">     100 </span><span class="lineCov">          4 :     if (loss_weight) {</span>
<span class="lineNum">     101 </span><span class="lineNoCov">          0 :       if (k == 0) {</span>
<span class="lineNum">     102 </span>            :         split_layer_param-&gt;add_loss_weight(loss_weight);
<span class="lineNum">     103 </span>            :       } else {
<span class="lineNum">     104 </span>            :         split_layer_param-&gt;add_loss_weight(0);
<span class="lineNum">     105 </span>            :       }
<span class="lineNum">     106 </span>            :     }
<span class="lineNum">     107 </span>            :   }
<a name="108"><span class="lineNum">     108 </span><span class="lineCov">          2 : }</span></a>
<span class="lineNum">     109 </span>            : 
<span class="lineNum">     110 </span><span class="lineCov">          2 : string SplitLayerName(const string&amp; layer_name, const string&amp; blob_name,</span>
<span class="lineNum">     111 </span>            :     const int blob_idx) {
<span class="lineNum">     112 </span><span class="lineCov">          4 :   ostringstream split_layer_name;</span>
<span class="lineNum">     113 </span><span class="lineCov">          2 :   split_layer_name &lt;&lt; blob_name &lt;&lt; &quot;_&quot; &lt;&lt; layer_name &lt;&lt; &quot;_&quot; &lt;&lt; blob_idx</span>
<span class="lineNum">     114 </span><span class="lineCov">          2 :       &lt;&lt; &quot;_split&quot;;</span>
<span class="lineNum">     115 </span><span class="lineCov">          2 :   return split_layer_name.str();</span>
<a name="116"><span class="lineNum">     116 </span>            : }</a>
<span class="lineNum">     117 </span>            : 
<span class="lineNum">     118 </span><span class="lineCov">          8 : string SplitBlobName(const string&amp; layer_name, const string&amp; blob_name,</span>
<span class="lineNum">     119 </span>            :     const int blob_idx, const int split_idx) {
<span class="lineNum">     120 </span><span class="lineCov">         16 :   ostringstream split_blob_name;</span>
<span class="lineNum">     121 </span><span class="lineCov">          8 :   split_blob_name &lt;&lt; blob_name &lt;&lt; &quot;_&quot; &lt;&lt; layer_name &lt;&lt; &quot;_&quot; &lt;&lt; blob_idx</span>
<span class="lineNum">     122 </span><span class="lineCov">          8 :       &lt;&lt; &quot;_split_&quot; &lt;&lt; split_idx;</span>
<span class="lineNum">     123 </span><span class="lineCov">          8 :   return split_blob_name.str();</span>
<a name="124"><span class="lineNum">     124 </span>            : }</a>
<span class="lineNum">     125 </span>            : 
<span class="lineNum">     126 </span><span class="lineCov">          2 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
